package org.absolutezero.client;

import javax.inject.Inject;

import org.absolutezero.client.events.LoginEvent;
import org.absolutezero.client.events.LoginEventHandler;
import org.absolutezero.client.events.RegisterEvent;
import org.absolutezero.client.events.RegisterEventHandler;
import org.absolutezero.client.events.RenderLoginEvent;
import org.absolutezero.client.events.RenderLoginEventHandler;
import org.absolutezero.client.events.RenderRegisterEvent;
import org.absolutezero.client.events.RenderRegisterEventHandler;
import org.absolutezero.client.events.RenderWelcomeEvent;
import org.absolutezero.client.events.RenderWelcomeEventHandler;
import org.absolutezero.client.presenter.PageContentPresenter;
import org.absolutezero.client.presenter.PageHeaderPresenter;
import org.absolutezero.client.util.Dialog;
import org.absolutezero.client.util.Session;
import org.absolutezero.client.view.PageContentView;
import org.absolutezero.client.view.PageView;
import org.absolutezero.shared.RefrigeratorRequestFactory;
import org.absolutezero.shared.UserProxy;
import org.absolutezero.shared.UserRequest;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;



public class RefrigeratorController {

	private final EventBus ebus;
	private final PageView page;
	private final PageContentView pageContent;
	private final PageHeaderPresenter pageHeaderPresenter;
	private final PageContentPresenter pageContentPresenter;
	private final RefrigeratorRequestFactory requestFactory;
	
	@Inject
	public RefrigeratorController(EventBus ebus,
					PageView page,
					PageContentView pageContent,
					PageContentPresenter pageContentPresenter,
					PageHeaderPresenter pageHeaderPresenter,
					RefrigeratorRequestFactory requestFactory){
		
		this.ebus = ebus;
		this.page = page;
		this.pageContent = pageContent;
		this.pageContentPresenter = pageContentPresenter;
		this.pageHeaderPresenter = pageHeaderPresenter;
		this.requestFactory = requestFactory;
		
		RootPanel.get("app-wrapper").add(page);
		
		requestFactory.initialize(ebus);
		
		bind();
		
	}
		
	
	private void bind(){
		
		ebus.addHandler(RenderWelcomeEvent.TYPE, new RenderWelcomeEventHandler() {
			
			@Override public void onEvent(RenderWelcomeEvent e) { onWelcome(); }
		});
		
		ebus.addHandler(RenderRegisterEvent.TYPE, new RenderRegisterEventHandler() {
			
			@Override public void onEvent(RenderRegisterEvent e) { onRegister(); }
		});
		
		ebus.addHandler(RenderLoginEvent.TYPE, new RenderLoginEventHandler() {
			
			@Override public void onEvent(RenderLoginEvent e) { onLogin(); }
		});
		
		ebus.addHandler(LoginEvent.TYPE, new LoginEventHandler(){

			@Override public void onEvent(LoginEvent e) { onLoginRequest(); }
			
		});
		
		ebus.addHandler(RegisterEvent.TYPE, new RegisterEventHandler(){

			@Override public void onEvent(RegisterEvent e) { onRegisterRequest(); }
			
		});
	}
	
	
	private void onWelcome(){
		pageHeaderPresenter.setNotLoggedHeaderButtons();
		pageContentPresenter.setWelcomeView();
		pageHeaderPresenter.hideNavigationMenu();
	}
	
	private void onLogin(){
		pageContentPresenter.setLoginView();
	}
	
	private void onRegister(){
		pageContentPresenter.setRegisterView();
	}
	
    private void onLoginRequest(){
		
		UserRequest request = requestFactory.userRequest();
		
		Request<UserProxy> loginRequest 
			= request.login(pageContent.getName(), pageContent.getPassword());
		
		loginRequest.fire(new Receiver<UserProxy>(){

			@Override
			public void onSuccess(UserProxy response) {
				
				pageContent.setShelvesView();
				pageHeaderPresenter.setLoggedHeaderButtons();
				pageHeaderPresenter.showNavigationMenu();
				
				Session.put("logged-user", response);
				
		
			}
			
			@Override
			public void onFailure(ServerFailure f){
				Dialog.show("User login failed",f.getMessage());
			}
			
		});
		
	}
	
	private void onRegisterRequest(){
		UserRequest request = requestFactory.userRequest();
		UserProxy user = request.create(UserProxy.class);
		
		user.setFullName(pageContent.getFullName());
		user.setLogin(pageContent.getName());
		user.setPassword(pageContent.getPassword());
		
		Request<Void> insertUserRequest = request.persist().using(user);
		
		insertUserRequest.fire(new Receiver<Void>(){

			@Override
			public void onSuccess(Void response) {
				pageContent.setRegisteredView();				
			}
			
			@Override
			public void onFailure(ServerFailure f){
				Window.alert(f.getMessage());
			}
			
		});
	}
	
		
}
